package com.superman.view;

import com.superman.entity.User;
import com.superman.util.Constants;
import com.superman.util.GUID;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RegisterUI {

    private JFrame frame = new JFrame("注册");
    private Container container = frame.getContentPane();
    private JTextField username = new JTextField();
    private JPasswordField password = new JPasswordField();
    private JComboBox sex = new JComboBox(Constants.sex);
    private JTextField id_number = new JTextField();
    private JTextField tel = new JTextField();
    private JTextField addr = new JTextField();

    private JButton confirm = new JButton("确定");
    private JButton cancel = new JButton("取消");

    public RegisterUI(){
        frame.setSize(360,500);
        container.setLayout(new BorderLayout());
        initFrame();
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    private void initFrame() {

        //顶部
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new FlowLayout());
        titlePanel.add(new JLabel("用户注册"));
        container.add(titlePanel,"North");

        //中部表单
        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(null);
        JLabel username_label = new JLabel("用户名:");
        username_label.setBounds(60, 40, 50, 20);
        JLabel password_label = new JLabel("密    码:");
        password_label.setBounds(60, 80, 50, 20);
        JLabel sex_label = new JLabel("性    别:");
        sex_label.setBounds(60, 120, 50, 20);
        JLabel id_number_label = new JLabel("身份证:");
        id_number_label.setBounds(60, 160, 50, 20);
        JLabel tel_label = new JLabel("电    话:");
        tel_label.setBounds(60, 200, 50, 20);
        JLabel addr_label = new JLabel("住    址:");
        addr_label.setBounds(60, 240, 50, 20);
        fieldPanel.add(username_label);
        fieldPanel.add(password_label);
        fieldPanel.add(sex_label);
        fieldPanel.add(id_number_label);
        fieldPanel.add(tel_label);
        fieldPanel.add(addr_label);
        username.setBounds(120,40,150,20);
        password.setBounds(120,80,150,20);
        sex.setBounds(120,120,150,20);
        id_number.setBounds(120,160,150,20);
        tel.setBounds(120,200,150,20);
        addr.setBounds(120,240,150,20);
        fieldPanel.add(username);
        fieldPanel.add(password);
        fieldPanel.add(sex);
        fieldPanel.add(id_number);
        fieldPanel.add(tel);
        fieldPanel.add(addr);
        container.add(fieldPanel,"Center");

        //底部按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(cancel);
        buttonPanel.add(confirm);
        container.add(buttonPanel,"South");

        confirm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String t_username = username.getText();
                String t_password = password.getText();
                int t_sex = (sex.getSelectedItem().equals("男"))? 0:1;
                String t_id_number = id_number.getText();
                String t_tel = tel.getText();
                String t_addr = tel.getText();
                User user = new User(GUID.getGUID(), t_username, t_password, t_sex, t_id_number, t_tel, t_addr, 0);
                //TODO
                hide();
                int row = new Register().register(user);
                if (row > 0){
                    new SuccessRegisterDialog(this, "注册成功提示", true);
                }else{
                    new FailRegisterDialog(this, "注册失败提示", true);
                }
            }
        });

        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //TODO
                hide();
                new LoginUI();
            }
        });
    }

    public void hide() {
        frame.setVisible(false);
    }


    //自定义注册成功提示对话框
    class SuccessRegisterDialog extends JDialog{
        public SuccessRegisterDialog(ActionListener owner, String title, boolean modal){
            init();
            this.setVisible(true);
        }

        private void init(){
            JPanel panel = new JPanel();
            Icon success = new ImageIcon("src\\com\\superman\\view\\icon\\success.png");
            JLabel desc = new JLabel("注册成功！", success, SwingConstants.CENTER);
            JButton btn = new JButton("返回登录");

            panel.add(btn);
            this.add(desc, BorderLayout.CENTER);
            this.add(panel, BorderLayout.SOUTH);
            this.setSize(300, 200);

            //设置弹出窗口位置
            int fx = frame.getLocation().x;
            int fy = frame.getLocation().y;
            int window_width = frame.getWidth();
            int window_height = frame.getHeight();
            int dialog_width = this.getWidth();
            int dialog_height = this.getHeight();
            this.setLocation(fx + (window_width - dialog_width)/2, fy + (window_height - dialog_height)/2);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    hide();
                    new LoginUI();
                }
            });
        }
    }

    //自定义注册失败提示对话框
    class FailRegisterDialog extends JDialog{
        public FailRegisterDialog(ActionListener owner, String title, boolean modal){
            init();
            this.setVisible(true);
        }

        private void init(){
            JPanel panel = new JPanel();
            Icon fail = new ImageIcon("src\\com\\superman\\view\\icon\\fail.png");
            JLabel desc = new JLabel("注册失败！请核对注册信息", fail, SwingConstants.CENTER);
            JButton register_button = new JButton("重新注册");
            JButton login_button = new JButton("返回登录");

            panel.add(register_button);
            panel.add(login_button);
            this.add(desc, BorderLayout.CENTER);
            this.add(panel, BorderLayout.SOUTH);
            this.setSize(300, 200);

            //设置弹出窗口位置
            int fx = frame.getLocation().x;
            int fy = frame.getLocation().y;
            int window_width = frame.getWidth();
            int window_height = frame.getHeight();
            int dialog_width = this.getWidth();
            int dialog_height = this.getHeight();
            this.setLocation(fx + (window_width - dialog_width)/2, fy + (window_height - dialog_height)/2);

            register_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    hide();
                    RegisterUI.this.frame.setVisible(true);
                }
            });

            login_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    hide();
                    new LoginUI();
                }
            });

        }
    }

}

